import pygame as pg
import sys
import time
import random

pg.font.init()
FPS = 60
WIDTH = 600
HEIGHT = 400
screen = pg.display.set_mode((WIDTH, HEIGHT)) 
COLOR_INACTIVE = pg.Color('lightskyblue3')
COLOR_ACTIVE = pg.Color('dodgerblue2')
FONT = pg.font.Font(None, 32)

class InputBox:
    def __init__(self, x, y, w, h, text=''):
        self.rect = pg.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = True
        self.answer = ''
        
    def handle_event(self, event):
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                    self.answer = self.text
                    self.text = ''
                else:
                    self.text += event.unicode
                self.txt_surface = FONT.render(self.text, True, self.color)
    
    def update(self):
        keys = pg.key.get_pressed()
        if keys[pg.K_BACKSPACE]:
            self.text = self.text[:-1]
        self.txt_surface = FONT.render(self.text, True, self.color)        
        width = max(200, self.txt_surface.get_width() + 10)
        self.rect.w = width    
    
    def draw(self, screen):
        screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 5))
        pg.draw.rect(screen, self.color, self.rect, 2)
                

words = ["import", "class", "def", "elif", "if x >= 10", "return"]
game_speed = [3, 6, 9]

class Word():
    def __init__(self):
        self.word = words[random.randint(0, len(words) - 1)]
        self.txt_surface = FONT.render(self.word, True, pg.Color('red'))
        self.rect = pg.Rect(WIDTH, random.randint(0,HEIGHT - 100),
            self.txt_surface.get_width(), self.txt_surface.get_height())
        self.velocity = 300
        self.alive = True
    
    def move(self, dt):
        self.rect.x -= self.velocity * dt
        if self.rect.x <= 0 - self.txt_surface.get_width():
            self.alive = False
    
    def draw(self, screen):
        screen.blit(self.txt_surface, (self.rect.x, self.rect.y))

class Score():
    def __init__(self, x, y):
        self.score = 0
        self.x = x
        self.y = y
    
    def increase(self):
        self.score += 1
    
    def draw(self, screen):
        screen.blit(FONT.render(str(self.score), True, COLOR_ACTIVE), (self.x, self.y))

def main():
    prev_time = time.time()
    clock = pg.time.Clock()
    input_box = InputBox(20, 350, 140, 32)
    done = False
    word_list = []
    time_counter = 3
    answered = False
    wrong_answer = False
    score = Score(300, 350)
    pg.time.set_timer(pg.USEREVENT, 1000)    

    while not done:
        
        now = time.time()
        dt = now - prev_time
        prev_time = now    
        
        for event in pg.event.get():
            if event.type == pg.USEREVENT:
                time_counter -= 1
                if time_counter <= 0:
                    word_list.append(Word())
                    time_counter = 3
            if event.type == pg.QUIT:
                done = True
            input_box.handle_event(event)
            
        input_box.update()
        wrong_answer = False
        screen.fill((30, 30, 30))
        
        for index, word in enumerate(word_list):
            if input_box.answer != '':
                wrong_answer = True
                if word.word == input_box.answer:
                    answered = True
                    word.alive = False
                    score.increase()
                    input_box.answer = ''
                input_box.answer = ''
            if word.alive == False:
                del word_list[index]
        
        if wrong_answer:
            screen.fill((123, 43, 50))
            screen.fill((30, 30, 30))
        
        for word in word_list:
            word.move(dt)
            word.draw(screen)
        score.draw(screen)
        input_box.draw(screen)
        #pg.display.flip()
        clock.tick(60)
        pg.display.update()
        
if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

